package com.students.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.students.entity.Student;
import com.students.mapper.StudentMapper;
import com.students.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author cd
 * @version 1.0
 * @date 2021/10/7 17:29
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void insert(Student student) {
        int num = studentMapper.insertSelective(student);
        if(num != 1){
            System.out.println("添加失败");
            throw new RuntimeException("添加失败");
        }

    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void update(Student student) {
        int num = studentMapper.updateByPrimaryKey(student);
        if(num != 1){
            System.out.println("修改失败");
            throw new RuntimeException("修改失败");
        }

    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void delete(Integer id) {
        int num = studentMapper.deleteByPrimaryKey(id);
        if(num != 1){
            System.out.println("删除失败");
            throw new RuntimeException("删除失败");
        }

    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Student findById(Integer id) {
        return studentMapper.findById(id);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public PageInfo<Student> findByClassId(Integer page, Integer rows, Integer ClassId) {
        PageHelper.startPage(page,rows);
        List<Student> studentList = studentMapper.findByClassIdAndPage(ClassId);
        PageInfo<Student> pageInfo = new PageInfo<>(studentList);
        return pageInfo;
    }
}